<template>
  <div id="lmain">
    <div class="showLoading">
      <van-loading type="spinner" color="#1989fa" size="50px"/>
      <div class="text">欢迎使用雅斯健康客服咨询，数据加载中，请稍后…</div>
    </div>
  </div>
</template>

<script>
import {getWxUser} from "@/api/api";

export default {
  name: "customer_consultation_loading",
  data() {
    return {
      token: "",
      code: '',
      accountCode: '',
      appid: '',
    }
  },
  mounted() {
    this.code = !this.getParam("code") ? "" : this.getParam("code");
    this.appid = !this.getParam("appid") ? "" : this.getParam("appid");
    this.accountCode = this.getParam("accountCode");
    console.log('打印拿到的链接参数', this.code, this.appid, this.accountCode)
    let type = this.$route.query.type
    let dyInfo = this.$route.query.userInfo
    if (dyInfo) {
      let userInfo = {
        userName: dyInfo.username,
        nickName: dyInfo.name,
        avatar: dyInfo.avatar
      }
      console.log('打印拿到的用户信息', userInfo)
      this.$store.commit('CHANGE_USERINFO', userInfo)
    }
    console.log('入口来源', type)
    if (type == 'khjlzx') {
      console.log('店员服务用户咨询进入')
      this.$router.replace({
        path: "/kzList",
        query: {
          accountCode: this.accountCode,
        },
      });
    } else if (type == 'txl') {
      console.log('店员服务通讯录过来的');
      this.$router.replace({
        path: "/kzTxl",
        query: {
          accountCode: this.accountCode,
        },
      });
    } else {
      this.getWxUserData();
    }
  },
  methods: {
    // 获取参数
    getParam(name) {
      if (window.location.search.indexOf("?") != -1) {
        const query =
            window.location.search.split("?")[1] +
            "&" +
            window.location.hash.split("?")[1];
        const vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split("=");
          if (pair[0] == name) {
            return pair[1];
          }
        }
      } else {
        return "";
      }
    },
    // 设置cookie,增加到vue实例方便全局调用
    setCookie(name, value, expiredays) {
      var exdate = new Date();
      exdate.setDate(exdate.getDate() + expiredays);
      document.cookie = name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    },
    // 获取用户是否关注公众号、登录等信息
    getWxUserData() {
      var formData = {
        state: "2222",
        code: this.code,
        accountCode: this.accountCode,
      };
      console.log('打印菜单链接拿到的参数', formData)
      getWxUser(formData)
          .then((response) => {
            const {data} = response;
            let userInfo = {
              userName: data.userName,
              nickName: data.nickName,
              avatar: data.avatar
            }
            console.log('打印拿到的用户信息', userInfo)
            this.$store.commit('CHANGE_USERINFO', userInfo)
            switch (data.code) {
              case "0":
                // 未关注公众号
                this.$router.replace({
                  path: "/fs",
                  query: {
                    accountCode: this.accountCode,
                    openId: data.openId,
                  },
                });
                break;
              case "4":
                // 该用户未注册，跳转到登录页面(注册即登录)
                this.$router.replace({
                  path: "/login",
                  query: {
                    openId: data.openId,
                    address: "kzList",
                    accountCode: this.accountCode,
                  },
                });
                break;
                //这种情况是 符合条件 跳转到客户咨询的列表
              case "2":
                this.token = JSON.parse(data.token).access_token;
                sessionStorage.setItem("token", this.token);
                this.$router.replace({
                  path: "/kzList",
                  query: {
                    openId: data.openId,
                    accountCode: this.accountCode,
                    address: "kzList",
                  },
                });
                break;
              default:
                console.log(data);
            }
          })
          .catch((e) => {
            console.log(e);
          });
      // this.$router.replace({
      //     path: "/ci",
      //     query: {},
      // });
    },
  }
}
</script>

<style scoped lang="less">
#lmain {
  min-width: 375px;

  .showLoading {
    width: 100vw;
    height: 100vh;
    position: fixed;
    top: 0;

    .van-loading {
      margin: 100px 162.5px;
    }

    .text {
      width: 60%;
      margin: 0 auto;
      text-align: center;
      font-size: 15px;
    }
  }
}
</style>